--
-- Author: shunguo.chen
-- Date: 2016-10-20 17:38:07
--

local ListViewExtend = {}

function ListViewExtend.scrollToItemExtend(listView)
    function listView:scrollToIndex(index, rate)
        local item = self:getItem(index)
        if cc.isAlive(item) then
            self:scrollToItem(item, rate)
        end
    end
    
    -- function listView:getVisibleItems()
    --     local items = self:getItems()
    --     if table.getn(items) <= 0 then return nil end

    --     local listHei = self:getContentSize().height
    --     local innerHei = self:getInnerContainer():getContentSize().height
    --     local itemSize = self:getItem(0):getContentSize()
    --     local innerPosY = self:getInnerContainer():getPositionY()
    --     local minY = -innerPosY - itemSize.height
    --     local maxY = -innerPosY + listHei + itemSize.height

    --     local visibleItems = {}
    --     for _, item in ipairs(items) do
    --         local itemY = item:getPositionY()
    --         if itemY >= minY and itemY <= maxY then
    --             table.insert(visibleItems, item)
    --         end
    --     end
    --     return visibleItems
    -- end

    -- 0    NONE,
    -- 1    VERTICAL,
    -- 2    HORIZONTAL,
    -- 3    BOTH
    function listView:scrollToItem(item, rate)
        -- print("ListView:scrollToItem()")
        local dir = self:getDirection()
        if dir == 0 then return end

        rate = rate or 0.5
        local viewSize = self:getContentSize()
        local innerSize = self:getInnerContainerSize()
        local i_posx, i_posy = self:getInnerContainer():getPosition()
        local innerPos = cc.p(self:getInnerContainer():getPosition())

        local ap = item:getAnchorPoint()
        local size = item:getContentSize()
        local posx, posy = item:getPosition()

        if dir == 1 then
            local offsetMax = innerSize.height - viewSize.height
            if offsetMax == 0 then return end

            local upMax = math.abs(i_posy)
            local downMax = offsetMax - upMax
            local cur = (posy + i_posy) + (0.5 - ap.y) * size.height
            local tar = rate * viewSize.height
            local dis = 0

            if cur == tar then

            elseif cur > tar then
                dis = -math.min(cur - tar, downMax)
            else
                dis = math.min(tar - cur, upMax)
            end

            if dis ~= 0 then 
                local per = (i_posy + dis) / offsetMax + 1
                self:scrollToPercentVertical(per * 100, 0.5, true)
            end
        elseif dir == 2 then
            local offsetMax = innerSize.width - viewSize.width
            if offsetMax == 0 then return end

            local rightMax = math.abs(i_posx)
            local leftMax = offsetMax - rightMax
            local cur = (posx + i_posx) + (0.5 - ap.x) * size.width
            local tar = rate * viewSize.width
            local dis = 0
            if cur == tar then

            elseif cur > tar then
                dis = -math.min(cur - tar, leftMax)
            else
                dis = math.min(tar - cur, rightMax)
            end

            if dis ~= 0 then 
                local per = math.abs(i_posx + dis) / offsetMax
                self:scrollToPercentHorizontal(per * 100, 0.5, true)
            end
        end
    end

    return listView
end

-- 移除list用不到的元素
-- listView ListView
-- len      需求item数
function ListViewExtend.removeUnuseItem(listView, len)
    -- 一屏之内的只隐藏不删除(以便重复使用),当前只处理上下滑动
    -- local size = listView:getContentSize()
    -- local items = listView:getItems()
    -- local innerHeight = listView:getInnerContainerSize().height
    -- local margin = listView:getItemsMargin()
    -- while #items > len do
    --     if innerHeight > size.height then
    --         local item = items[#items]
    --         innerHeight = innerHeight - item:getContentSize().height - margin
    --         listView:removeLastItem()
    --         items = listView:getItems()
    --     else
    --         for i=len+1,#items do
    --             items[i]:setVisible(false)
    --         end
    --         break
    --     end
    -- end

    -- 全部移除不需要的item
    local items = listView:getItems()
    if len < #items then
        for i=1, #items-len do
            listView:removeLastItem()
        end
    end
end

-- 隐藏 listView里面看不到的元素
function ListViewExtend.adjustItemsVisible(listView)
    if not listView then return end
    -- listView:setClippingEnabled(false)
    local itemNode = listView:getItem(0)
    if not itemNode then return end
    local oneHeight = itemNode:getContentSize().height + listView:getItemsMargin()
    local visibleNum = math.ceil(listView:getContentSize().height / oneHeight)

    local function scrollViewEvent(sender, evenType)
        if evenType == ccui.ScrollviewEventType.scrolling then
            local innerY = sender:getInnerContainer():getPositionY()
            if innerY > 0 then
                innerY = 0
            end
            local bottomNum = math.floor(math.abs(innerY) / oneHeight)
            local children = sender:getChildren()

            local totalNum = #children
            local startIndex = totalNum - (bottomNum + visibleNum)
            if startIndex < 0 then
                startIndex = 0
            end
            startIndex = startIndex
            local endIndex = startIndex + visibleNum

            local function setVisibility(index,child)
                if index >= startIndex and index <= endIndex then
                    child:setVisible(true)
                else
                    child:setVisible(false)
                end
            end
            for i = #children,1,-1 do
                local child = children[i]
                if i == totalNum then
                    setVisibility(i,child)
                elseif child:getPositionY() ~= 0 then
                    setVisibility(i,child)
                end
            end
        end
    end
    listView:addScrollViewEventListener(scrollViewEvent)
end


return ListViewExtend